package Control;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.User;

/**
 * Servlet implementation class QuanLyNguoiDung
 */
public class QuanLyNguoiDung extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QuanLyNguoiDung() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		if (action.equalsIgnoreCase("adduser")) {
			String username = request.getParameter("username");
			String pass = request.getParameter("hash");
			String name = request.getParameter("name");
			String address = request.getParameter("address");
			String province = request.getParameter("province");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String roleid = request.getParameter("roleid");

			boolean res = User.addUser(username, pass, name, address, province,
					phone, email, roleid);
			if (res == true) {
				response.sendRedirect("Admin/AccountManage.jsp");
			}

		}

		else if (action.equalsIgnoreCase("deleteUser")) {
			String userID = request.getParameter("UserID");
			// search user
			String url_search_xoa_user  = request.getParameter("url_search_xoa_user");
			System.out.println("Xoa User: " + userID);
			int iUserID = 0;
			boolean res = false;
			try {
				iUserID = Integer.parseInt(userID);
				res = User.deleteUser(iUserID);
			} catch (Exception e) {
			}

			String url = "/Admin/Error.jsp";
			
				if (res == true) {
					if(url_search_xoa_user != null){
						url = "/QuanLyNguoiDung";
					}else{
						url = "/Admin/AccountManage.jsp";
					}
				}

			RequestDispatcher re = getServletContext()
					.getRequestDispatcher(url);
			re.forward(request, response);
		}

		else if (action.equalsIgnoreCase("viewUser")) {
			String userID = request.getParameter("UserID");
			int iUserID = 0;
			User user = null;
			try {
				iUserID = Integer.parseInt(userID);
				user = User.getInfoUser(iUserID);
				if (user != null) {
					request.setAttribute("infoUser", user);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			String url = "/Admin/Error.jsp";
			if (user != null) {
				url = "/Admin/UserDetail.jsp";
			}

			RequestDispatcher re = getServletContext()
					.getRequestDispatcher(url);
			re.forward(request, response);
		}

		else if (action.equalsIgnoreCase("searchUser")) {
			String key_username = request.getParameter("username");
			System.out.println("Tim Kiem User: " + key_username);
			String page = request.getParameter("page");
			
			int countRecord = User.countUserSearch(key_username); 
			int numRecord = 5;
			int ipage = 1;

			try {
				if (page != null) {
					ipage = Integer.parseInt(page);
				}
				ArrayList<User> listResult = User.searchUser(key_username, ipage, numRecord);
				request.setAttribute("listResultSerch", listResult);
				request.setAttribute("key_username", key_username);
				request.setAttribute("countRecord", countRecord);

				//				for (User user : listResult) {
//					System.out.println(user.toString());
//				}

				RequestDispatcher re = getServletContext().getRequestDispatcher("/Admin/AccountManage.jsp");
				re.forward(request, response);

			} catch (Exception e) {
				// TODO: handle exception

			}
		}

		else if (action.equalsIgnoreCase("manageUser")) {
			String page = request.getParameter("page");
			int ipage = 1;
			int numRecord = 5;
			try {
				ipage = Integer.parseInt(page);
				ArrayList<User> listResult = User.getAllUser(numRecord, ipage);
				request.setAttribute("listResult", listResult);
				for (User user : listResult) {
					System.out.println(user.toString());
				}
				RequestDispatcher re = getServletContext()
						.getRequestDispatcher("/Admin/AccountManage.jsp");
				re.forward(request, response);

			} catch (Exception e) {
				// TODO: handle exception

			}
		}

	}

}
